// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import Debuger from "../../utils/Debuger";
import MeshRender2D from "./libs/MeshRender2D";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    @property(MeshRender2D)
    circleMesh: MeshRender2D = null;

    @property(MeshRender2D)
    rectMesh: MeshRender2D = null;

    private _isShowMesh: boolean = false;

    clickMesh() {
        this._isShowMesh = !this._isShowMesh;
    }

    protected lateUpdate(dt: number): void {
        Debuger.getGraphcis().clear();
        if (this._isShowMesh) {
            this.drawMesh(this.circleMesh);
            this.drawMesh(this.rectMesh);
        }
    }

    private drawMesh(mesh: MeshRender2D) {
        let graphics = Debuger.getGraphcis();
        graphics.fillColor = cc.Color.RED;
        let vertices = mesh["_assembler"]._renderData.vDatas[0];
        let hW = cc.winSize.width * 0.5;
        let hH = cc.winSize.height * 0.5;
        //画点
        for (let i = 0; i < vertices.length; i = i + 5) {
            graphics.circle(vertices[i] - hW, vertices[i + 1] - hH, 2);
            graphics.fill();
        }

        graphics.strokeColor = cc.Color.WHITE;
        graphics.lineWidth = 2;
        //画线
        for (let i = 0; i < mesh.indices.length; i = i + 3) {
            let i1 = mesh.indices[i];
            let i2 = mesh.indices[i + 1];
            let i3 = mesh.indices[i + 2];
            graphics.moveTo(vertices[i1 * 5] - hW, vertices[i1 * 5 + 1] - hH);
            graphics.lineTo(vertices[i2 * 5] - hW, vertices[i2 * 5 + 1] - hH);
            graphics.lineTo(vertices[i3 * 5] - hW, vertices[i3 * 5 + 1] - hH);
            graphics.lineTo(vertices[i1 * 5] - hW, vertices[i1 * 5 + 1] - hH);
        }
        graphics.stroke();
    }
}
